package DAL;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import entities.ConnectionManager;
import entities.UserPass;

public class UserPassDA {
	public UserPass[] selectUserPasses(UserPass UserPass)
	{
		boolean whereClause = false;
		String query = "SELECT * from UserPass  " ;
		if(UserPass.getUserID() != -1)
		{
			query += "WHERE UserID = " + UserPass.getUserID();
			whereClause = true;
		}

		if(!UserPass.getPasswordHash().equals("")) 
		{
			if(whereClause)
				query += " AND passwordHash = '" + UserPass.getPasswordHash() + "'";
			else
				query += " WHERE passwordHash = '" + UserPass.getPasswordHash() + "'";
			whereClause = true;
		}
		if(!UserPass.getSalt().equals("")) 
		{
			if(whereClause)
				query += " AND Salt = '" + UserPass.getSalt() + "'";
			else
				query += " WHERE Salt = '" + UserPass.getSalt() + "'";
			whereClause = true;
		}

		ConnectionManager con = new ConnectionManager();
		ResultSet rs = con.executeSelectQuery(query);
		
		ArrayList<UserPass> UserPasses = new ArrayList<UserPass>();

		
		try {
			while(rs.next())
			{
				UserPass temp= new UserPass();
				temp.setUserID(rs.getInt("UserID"));
				temp.setPasswordHash(rs.getString("passwordHash"));
				temp.setSalt(rs.getString("salt"));
				UserPasses.add(temp);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		UserPass [] temp = new UserPass [0];
		return UserPasses.toArray(temp);

	}


	public boolean updateUserPass(UserPass UserPass)
	{
		String query = "UPDATE UserPass SET " ;
		if(!UserPass.getPasswordHash().equals("")) 
		{
			query += " passwordHash = '" + UserPass.getPasswordHash() + "'";
			if(!UserPass.getSalt().equals("")) 
			{
				query += " , salt = '" + UserPass.getSalt() + "'";
			}
		}
		else
		{
			if(!UserPass.getSalt().equals("")) 
			{
				query += " salt = '" + UserPass.getSalt() + "'";
			}
		}
		
		query += " WHERE UserID = "+ UserPass.getUserID() ;
		
		ConnectionManager con = new ConnectionManager();
		
		int x = con.executeUpdateQuery(query);
		
		return (x==1);
	}
	
	public boolean insertUserPass(UserPass UserPass)
	{
		String query = "INSERT INTO UserPass ( UserID , PasswordHash , salt) VALUES " +
				"(" + UserPass.getUserID() + " ,  '"+ UserPass.getPasswordHash()+ "' , '" + UserPass.getSalt()+ "' )";
		
		ConnectionManager con = new ConnectionManager();
		
		int x = con.executeUpdateQuery(query);
		
		return x==1;
	}
	
	public int deleteUserPass(UserPass UserPass)
	{
		boolean whereClause = false;
		String query = "DELETE FROM UserPass ";
		if(UserPass.getUserID() != -1)
		{
			query += "WHERE UserID = " + UserPass.getUserID();
			whereClause = true;
		}
		
		if(!UserPass.getPasswordHash().equals("")) 
		{
			if(whereClause)
				query += " AND passwordHash = '" + UserPass.getPasswordHash() + "'";
			else
				query += " WHERE passwordHash = '" + UserPass.getPasswordHash() + "'";
			whereClause = true;
		}
		
		if(!UserPass.getSalt().equals("")) 
		{
			if(whereClause)
				query += " AND salt = '" + UserPass.getSalt() + "'";
			else
				query += " WHERE salt = '" + UserPass.getSalt() + "'";
			whereClause = true;
		}
		ConnectionManager con = new ConnectionManager();
		
		int x = con.executeUpdateQuery(query);
		
		return x;
	}	
	
	public static void main(String[] args) {
		UserPass u = new UserPass();

		
		//u.setEmail("asdsadasd");
		//u.setPasswordHash("AdminPasddasass121asd");
		//u.setId(2);
		u.setUserID(1);
		u.setPasswordHash("1111asdsaaasddadsa");
		u.setSalt("asdasd");
		UserPassDA da = new UserPassDA();
		System.out.println(da.deleteUserPass(u));
	}

}
